<?php 
	session_start();
	$var1= $_SESSION["registrado"];
	$var2= $_SESSION["administrador"];
	if ($var1 == null): {
		echo ("User not registered");
	} elseif ($var2 == 0): { 
			echo ("User without permissions");
	} else : {
?>

<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">

<head>
<?php include ('configuration.php'); ?>	
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">

<link href="css/bootstrap.css" rel="stylesheet" media="screen">
<link href="css/bootstrap-responsive.css" rel="stylesheet"	media="screen">

<title>My project manager</title>



<style type="text/css">
  body {
    padding-top: 60px;
    padding-bottom: 40px;
  }
  .sidebar-nav {
    padding: 9px 0;
  
  }
  .container {
    padding-top: 100px;
  }

  .finish {
    padding-top: 200px;
    padding-left: 25px;
    padding-right: 25px;
 }

  @media (max-width: 980px) {
    /* Enable use of floated navbar text */
    .navbar-text.pull-right {
      float: none;
      padding-left: 5px;
      padding-right: 5px;
    }
  }
</style>


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrap.js"></script>
   	
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
   	
    google.load('visualization', '1', {packages: ['corechart']});

    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawVisualization);

    function drawVisualization() {
	    <?php
	        $page=$_GET['page'];
	        $aux=10*$page;
	        $message=0;
	        $no_actualizar = 0;
	    	$ini1 = $_SESSION['ini1'];
			$ini2 = $_SESSION['ini2'];
			$fin1 = $_SESSION['fin1'];
			$fin2 = $_SESSION['fin2'];
			$q= mysql_query("select p.id_project, p.name_project, p.id_version, p.advantage, p.initial_date, p.final_date
		                                    from project p where p.id_version=(select max(P2.id_version)
		                                                      from Project P2
		                                                      where P.id_project=P2.id_project and P.deleted=0)
															  and (p.initial_date >= '$ini1' and p.initial_date <= '$ini2') 
															  and (p.final_date >= '$fin1' and p.final_date <= '$fin2')
											order by p.initial_date asc LIMIT $aux, 100"); 
			$num_resultados = mysql_num_rows($q);
			$j = 0;
			$vueltas=0;
			
			if ($num_resultados > 10) {
		   		$num_grupos_diez = ceil($num_resultados / 10);
		   		$vueltas=10;
		   	}
		   	else {$vueltas=$num_resultados;}
	   		
		     $myRow = null;
		     $i = 0;
	   		 
   		 	 while ($i < $vueltas) {
   		 	 	$res = mysql_result($q, $i);
			 	$q1 = mysql_query("select max(id_version) from project where id_project = '$res'");
			 	$res2 = mysql_fetch_assoc($q1);
			 	$version = $res2['max(id_version)'];
			 	$query = mysql_query("SELECT id_project, name_project, deleted, id_version, advantage, initial_date, final_date
			 							 FROM project WHERE id_project = '$res' AND id_version = '$version'");
            	$row = mysql_fetch_assoc($query);
            	$name_proj = $row['name_project'];
            	
            	$initial_proj = $row['initial_date'];
            	$anyoInicial = substr($initial_proj, 0, 4);
				$aux5 = substr($initial_proj, 5, 6);
				$mesInicial = substr($aux5, 0, 2);
				$diaInicial = substr($initial_proj, 8, 9);
            	$initial_num = $anyoInicial . $mesInicial . $diaInicial;
            	
            	$final_proj = $row['final_date'];
            	$anyoFinal = substr($final_proj, 0, 4);
				$aux6 = substr($final_proj, 5, 6);
				$mesFinal = substr($aux6, 0, 2);
				$diaFinal = substr($final_proj, 8, 9);
				$final_num = $anyoFinal . $mesFinal . $diaFinal;
            	
            	$adv_proj = $row['advantage'];
            	$adv_num = ((($final_num - $initial_num)/ 100) * $adv_proj) + $initial_num;
            	
			 	if ($i == 0) {
			 		$myRow = $myRow . "[" . "\n";
			 		$myRow = $myRow . "['NOMBRE_PROJ', 'FECHA_INI', 'FECHA_FIN_AVANCE', 'FECHA_FIN_AVANCE', 'FECHA_FIN']," . "\n";
			 	}
			 	$myRow = $myRow . "['$name_proj', $initial_num, $initial_num, $adv_num, $final_num]";		
			 	if ($i != ($vueltas-1) ) {
			 		$myRow = $myRow . "," . "\n";
			 	} else {
			 		$myRow = $myRow . "\n". "]";
			 	}
			 	$i = $i + 1;
			 }	
			 
			 if ($num_resultados == 0) {
			 	$message = 2;
			 } else {
			 	?>
    			var dataTable = google.visualization.arrayToDataTable(<?php echo $myRow; ?>, false);	    
	        	// Draw the chart.
	       		var chart = new google.visualization.CandlestickChart(document.getElementById('visualization'));
	        	chart.draw(dataTable, {legend:'none', width:600, height:400});
	        	<?php       	
 	 		}	
		?>
    }
</script>
</head>
<body>

<div class="navbar navbar-inverse navbar-fixed-top">
	<div class="navbar-inner">
		<div class="container-fluid">
			<button type="button" class="btn btn-navbar" data-toggle="collapse"
				data-target=".nav-collapse">
				<span class="icon-bar"></span> <span class="icon-bar"></span> <span
					class="icon-bar"></span>
			</button>
			<a class="brand" href="http://localhost/bases--grupo10/homeAdmin.php?">My project manager</a>
			<div class="nav-collapse collapse">
				<p class="navbar-text pull-right">
				
					<a href="http://localhost/bases--grupo10/logout.php?" class="navbar-link">Loggout</a>
				</p>
				<ul class="nav">
					<li class="active"><a href="http://localhost/bases--grupo10/homeAdmin.php?">Home</a></li>
					<li><a href="http://localhost/bases--grupo10/about.php?">About</a></li>
					<li><a href="http://localhost/bases--grupo10/contact.php?">Contact</a></li>
				</ul>
			</div>
		</div>
	</div>
</div>
	
	
   <?php 	
	//no ha habido fallo, se muestra el grafico
	$num_grupos_diez = 0;
	$pageAux1 = 0;
	$pageAux2 = 0;
	$aux = 0;

   	if ($no_actualizar == 0) { ?>
		<div class="span12">
		    <h1 class="muted">Graphs > </h1>
		    <h1 style=font-weight:bold"><font size="5"><u> Planning and progress of the projects</u></font></h1>
		</div>	
	
		<?php 
		$q= mysql_query("select p.id_project, p.name_project, p.id_version, p.advantage, p.initial_date, p.final_date
	                                    from project p where p.id_version=(select max(P2.id_version)
	                                                      from Project P2
	                                                      where P.id_project=P2.id_project and P.deleted=0)
														  and (p.initial_date >= '$ini1' and p.initial_date <= '$ini2') 
														  and (p.final_date >= '$fin1' and p.final_date <= '$fin2') LIMIT $aux, 100"); 
		$num_resultados = mysql_num_rows($q);
		$j = 0;
		
		if ($num_resultados > 10) {
	   		$num_grupos_diez = ceil($num_resultados / 10);	
	   	} ?>
   		
   		<div class="span12 offset3">
		    <div id="visualization" style="font-family: Arial;border: 0 none;"></div> 
		</div>
		
   		 <?php 
   		 if ($num_grupos_diez > 1 && $page > 0 && $page < $num_grupos_diez - 1) {
			$aux = $page;
			$pageAux2 = $aux + 1;
			$pageAux1 = $aux - 1; ?>
			<a class='btn btn-primary' type='button' href="http://localhost/bases--grupo10/graphs2_next.php?page=<?php echo $pageAux1;?>" style='position:absolute;top:470px;left:250px'>Previous</a>
			<a class='btn btn-primary' type='button' href="http://localhost/bases--grupo10/graphs2_next.php?page=<?php echo $pageAux2;?>" style="position:absolute;top:470px;left:875px">Next</a>
			<?php 
		} elseif ($page>0) { ?>
			<a class='btn btn-primary' type='button' href="http://localhost/bases--grupo10/graphs2_next.php?page=<?php echo --$page;?>" style='position:absolute;top:470px;left:250px'>Previous</a>
			<?php 
   		} elseif ($num_grupos_diez>1) { ?>
			<a class='btn btn-primary' type='button' href="http://localhost/bases--grupo10/graphs2_next.php?page=<?php echo ++$page;?>" style='position:absolute;top:470px;left:875px'>Next</a>
			<?php 
   		} 

		if ($message == 2) {
			echo "<h1 style='position:absolute;top:160px;left:50px;font-weight:bold'><font size='5'><u>Don't exist any last version of the projects in the dates you have introduced</u></font></h1>";
		}
		?>
	
		<div class="finish">  	
		    <a class="btn btn-primary pull-right" type="button" href="http://localhost/bases--grupo10/homeAdmin.php?">Finish</a>  	
		    <a class="btn btn-primary pull-left" type="button" href="http://localhost/bases--grupo10/graphs.php?" >Back</a>
	    </div>
		<?php 
   	} ?>

	
</body>
</html>

<?php }endif;?>
